

package com.controller;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.service.UserService;
import java.util.List;
import com.model.User;
import javax.servlet.RequestDispatcher;

public class LoginController extends HttpServlet {
   
 
 User user = null;
 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

                  if(request.getParameter("userName") != null){

                  request.setAttribute("userName",request.getParameter("userName") );
                  RequestDispatcher dispatcher = request.getRequestDispatcher("/guess.jsp");
                  dispatcher.forward(request, response);
                  
                  }
    


    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

       
        try {


            String userName = request.getParameter("userName");
            String password = request.getParameter("password");
            UserService userService = new UserService();

            boolean error = false;
            
             if ( userName.length() == 0 || userName == null) {
                error = true;
            }
             if ( password.length()  == 0 || password == null) {
                error = true;
            }


            if (error) {
                request.setAttribute("errorMsg", "All Fields are required !");
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
                requestDispatcher.forward(request, response);

            }


            userService.setConnection();

            List<User> list = userService.getAll();
            
            userService.closeConnection();
            

            for ( int i=0; i < list.size(); i++ ) {

                User user1 = list.get(i);

                if(user1.getUserName().equals(userName) && user1.getPassword().equals(password)){

                    user = user1;
                    break;
                    
                }
                
            }

            if(user == null){

                request.setAttribute("Msg", "Please check your Details!");
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
                requestDispatcher.forward(request, response);
                
            }
            else if(user != null){


                for(int i = 0; i < list.size() ;i++){

                    User eachUser = list.get(i);
                    eachUser.setActive(false);
                    
                }

                user.setActive(true);


                request.setAttribute("user", user);
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/loginSuccess.jsp");
                requestDispatcher.forward(request, response);

              
            }



        } catch (ClassNotFoundException ex) {

            ex.printStackTrace();
        } catch (SQLException ex) {

            ex.printStackTrace();
        }



    }

  
  
}
